/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dmcn.bl.genetics.gui;

import dmcn.bl.genetics.input.Input;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jgap.IChromosome;

/**
 *
 * @author Spyros
 */
public class GuiController extends Canvas {

    int generations = 1;
    private Input input;
    private IChromosome bestSolutionSoFar;
    private JFrame container;
    private int width = 600;
    private int height = 500;
    ConcetratorGui[] concetrators;
    TerminalGui[] terminals;
    private BufferStrategy strategy;

    public GuiController(Input input) {
        this.input = input;
        initWindow();
        requestFocus();
        initGraphics();
        initEntities();
    }

    private void initWindow() {
        container = new JFrame("Genetics");

        JPanel panel = (JPanel) container.getContentPane();
        panel.setPreferredSize(new Dimension(width, height));
        panel.setLayout(null);

        setBounds(0, 0, width, height);
        panel.add(this);
        panel.setAlignmentX(CENTER_ALIGNMENT);
        panel.setAlignmentY(CENTER_ALIGNMENT);

        setIgnoreRepaint(true);

        container.setLocation(100, 150);
        container.pack();
        container.setResizable(false);
        container.setVisible(true);

        container.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    private void initGraphics() {
        createBufferStrategy(2);
        strategy = getBufferStrategy();
    }

    private void initEntities() {
        concetrators = new ConcetratorGui[input.getSygkentrotes()];
        terminals = new TerminalGui[input.getPhges()];
        int concetratorSpan = width / (concetrators.length + 1);
        for (int i = 0; i < concetrators.length; i++) {
            ConcetratorGui concetratorGui = new ConcetratorGui(
                    i,
                    input.getXwritikothta()[i],
                    (i + 1) * concetratorSpan,
                    height / 2,
                    50,
                    50);
            concetrators[i] = concetratorGui;
        }
        int terminalSpan = width / (terminals.length + 1);
        for (int i = 0; i < terminals.length; i++) {
            TerminalGui terminalGui = new TerminalGui(
                    i,
                    input.getFortio()[i],
                    (i + 1) * terminalSpan,
                    height / 4,
                    20,
                    20);
            terminals[i] = terminalGui;
        }
    }

    public void paint() {
        log();
        paint2d();
    }

    private void log() {
        System.out.println("The best solution has a fitness value of "
                + bestSolutionSoFar.getFitnessValue());
        System.out.print("   ");
        //ids
        for (int i = 0; i < input.getPhges(); i++) {
            System.out.print((i + 1) + " ");
        }
        System.out.println("");
        int j = 0;
        for (int i = 0; i < bestSolutionSoFar.getGenes().length; i++) {
            //id
            if ((i) % input.getPhges() == 0) {
                System.out.print(++j + ": ");
            }
            //value
            System.out.print(bestSolutionSoFar.getGenes()[i].getAllele() + " ");
            //change row
            if ((i + 1) % input.getPhges() == 0) {
                System.out.println("");

            }

        }

    }

    private void paint2d() {
        Graphics2D g2d = (Graphics2D) strategy.getDrawGraphics();

        //bg color
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);

        g2d.setColor(Color.black);
        g2d.drawString("best fitness value: " + bestSolutionSoFar.getFitnessValue(), 5, 20);
        g2d.drawString("generations: " + generations++, 5, 30);
        
        int[][] solutionArray = new int[concetrators.length][terminals.length];
        int c = 0, t = 0;
        for (int i = 0; i < bestSolutionSoFar.getGenes().length; i++) {
            solutionArray[c][t] = ((Integer) bestSolutionSoFar.getGenes()[i].getAllele()).intValue();
            t++;
            if ((i + 1) % input.getPhges() == 0) {
                c++;
                t = 0;
            }
        }
        for (int i = 0; i < concetrators.length; i++) {
            //draw concetrator
            g2d.setColor(Color.red);
            ConcetratorGui concetratorGui = concetrators[i];
            g2d.draw(concetratorGui);
            g2d.drawString(concetratorGui.getXwritikothta() + "", concetratorGui.x + 20, concetratorGui.y + 20);
            for (int j = 0; j < terminals.length; j++) {
                if (solutionArray[i][j] == 1) {//if connects
                    //draw terminal
                    TerminalGui terminalGui = terminals[j];
                    g2d.setColor(Color.blue);
                    g2d.draw(terminalGui);
                    g2d.drawString(terminalGui.getFortio() + "", terminalGui.x + 4, terminalGui.y + 14);
                    //draw line
                    g2d.setColor(Color.black);
                    g2d.drawLine(terminalGui.x, terminalGui.y, concetratorGui.x, concetratorGui.y);
                }
            }

        }

        // clear up the graphics
        // and flip the buffer over
        g2d.dispose();
        strategy.show();
    }

    public IChromosome getBestSolutionSoFar() {
        return bestSolutionSoFar;
    }

    public void setBestSolutionSoFar(IChromosome bestSolutionSoFar) {
        this.bestSolutionSoFar = bestSolutionSoFar;
    }
}
